<?php
require_once 'Sabai/Controller/Front.php';

class Xigg_Admin extends Sabai_Controller_Front
{
	function Xigg_Admin()
	{
		parent::Sabai_Controller_Front('Category', 'Xigg_Admin_', dirname(__FILE__) . '/Admin');
		$this->setFilters(array('global', 'isAuthenticated', 'isAdmin'));
	}

	function _getRoutes()
	{
		$routes['category'] = array('controller' => 'Category');
		$routes['node'] = array('controller' => 'Node');
		$routes['tag'] = array('controller' => 'Tag');
		$routes['plugin'] = array('controller' => 'Plugin');
		return $routes;
	}

	function globalBeforeFilter(&$context)
    {
        $plugin_mngr =& $this->_locator->getService('PluginManager');
        $plugin_mngr->dispatch('EnterAdmin', array(&$context));
    }

    function globalAfterFilter(&$context)
    {
        $plugin_mngr =& $this->_locator->getService('PluginManager');
        $plugin_mngr->dispatch('ExitAdmin', array(&$context));
    }

	function isAuthenticatedBeforeFilter(&$context)
    {
        if (!$context->user->isAuthenticated()) {
            $context->response->setError(_('You must login to perform this operation'), Sabai_URL::get('/login'));
            $context->response->send();
            exit;
        }
    }

	function isAdminBeforeFilter(&$context)
    {
        if (!$context->user->isInRole(XIGG_ROLE_ADMIN)) {
            $context->response->setError(_('Access denied'), Sabai_URL::getByScript());
            $context->response->send();
            exit;
        }
    }
}